<template>
  <a-drawer
    :title="title"
    :maskClosable="true"
    :width="drawerWidth"
    placement="right"
    :closable="true"
    @close="handleCancel"
    :visible="visible"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="开通时间">
          <a-select v-model="yearDate">
            <a-select-option
              v-for="item in years"
              :label="item.label"
              :value="item.value">
              {{item.value}}年
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="服务器类别">
<!--          <j-select-depart v-decorator="['office', validatorRules.office]"/>-->
          <a-input placeholder="请输入服务器类别" v-decorator="[ 'category', validatorRules.category]" />
        </a-form-item>
        <a-form-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="服务器名称">
          <j-dict-select-tag :triggerChange="true" ref="serverName" placeholder="请选择服务器名称" @change="serverChange" dictCode="serverName" v-decorator="[ 'serverValue', validatorRules.serverValue]"/>
<!--          <a-input placeholder="服务器名称"  v-decorator="[ 'serverName', validatorRules.serverName]" />-->
        </a-form-item>
        <div id="aaa"  v-show="isShow">
        <a-row :gutter="20">
            <a-form-item label="服务器参数"  :labelCol="labelCol"
                         :wrapperCol="wrapperCol" >
            </a-form-item>
          </a-row>
        <a-row :gutter="20">
          <a-col :md="8">
              <a-form-item
                :labelCol="labelRow"
                :wrapperCol="wrapperRow"
                label="CPU">
                <j-dict-select-tag :triggerChange="true"  :disabled="false" placeholder="请选择CPU" ref="cpu" dictCode="cpu" v-decorator="[ 'cpu', validatorRules.cpu]"/>
              </a-form-item>
          </a-col>
          <a-col :md="8">
            <a-form-item
              :labelCol="labelRow"
              :wrapperCol="wrapperRow"
              label="内存(G)">
              <j-dict-select-tag :triggerChange="true" :disabled="false" placeholder="请选择内存" ref="disk" dictCode="disk"  v-decorator="[ 'disk', validatorRules.disk]"/>
            </a-form-item>
          </a-col>
          <a-col :md="8">
            <a-form-item
              :labelCol="labelRow"
              :wrapperCol="wrapperRow"
              label="硬盘(G)">
              <j-dict-select-tag :triggerChange="true" placeholder="请选择硬盘" :disabled="false" ref="hardpan" dictCode="hardpan" v-decorator="[ 'hardpan', validatorRules.hardpan]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <!--<a-row :gutter="24">-->
        <!--</a-row>-->
        </div>
        <a-row :gutter="24">
<!--          <a-col :md="8">-->
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="计费要素">
              <a-select v-decorator="['elements', {}]" placeholder="请选择计费要素">
                <a-select-option value="">请选择计费要素</a-select-option>
                <a-select-option value="1">台</a-select-option>
                <a-select-option value="2">核</a-select-option>
                <a-select-option value="3">GB</a-select-option>
              </a-select>
            </a-form-item>
<!--          </a-col>-->
        </a-row>
        <a-row :gutter="24">
<!--          <a-col :md="8">-->
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="资费">
              <a-input placeholder="资费"  v-decorator="[ 'expense', validatorRules.expense]" />(元/年)

            </a-form-item>
<!--          </a-col>-->
        </a-row>
      </a-form>
    </a-spin>
    <div class="drawer-bootom-button" v-show="!disableSubmit">
      <a-popconfirm title="确定放弃编辑？" @confirm="handleCancel" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">返回</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="confirmLoading">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
  import pick from 'lodash.pick'
  import {saveServerPriceList,editServerPriceList } from '@/api/api'
  import JMultiSelectTag from '@/components/dict/JMultiSelectTag'
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  export default {
    components: {
      JMultiSelectTag,
      JSelectDepart,
    },
    name: "cloudServerPriceListModel",
    data(){
      return{
        isShow: true,
        yearDate:'',
        years:[],
        form: this.$form.createForm(this),
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 14 },
          sm: { span: 2 },
        },
        wrapperCol: {
          xs: { span: 14 },
          sm: { span: 6 },
        },
        labelRow: {
          xs: { span: 14 },
          sm: { span: 10 },
        },
        wrapperRow: {
          xs: { span: 14 },
          sm: { span: 14 },
        },
        dis:false,
        hiding:true,
        drawerWidth:1000,
        confirmLoading: false,
        disableSubmit:false,
        validatorRules:{},
      }
    },
    mounted() {
      this.init();
    },
    methods:{
      serverChange(e){
        if( e =='5'|| e=='6'|| e=='7'){
          this.isShow = false;

        }else{
          this.isShow = true ;
        }

      },
      init(){
        this.getYear();
      },
      getYear(){//获取年
        let date = new Date();
        let current_year = date.getFullYear();
        // this.queryParam.yearDate=current_year;
        this.years=[];
        for (let i = 0; i < 30; i++) {
          let y = current_year - i;
          this.years.push({value:(y),label:(y)+ "年"});
          // this.yearDate.push(y);
          if(y=='1970'){
            return
          }
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.disableSubmit = false;
      },
      add () {
        this.edit({});
      },
      edit(record){
        let serverValue=record.serverValue;
        if(serverValue =='5' || serverValue=='6' || serverValue=='7'){
          this.isShow = false;
        }else {
          this.isShow=true;
        }
        if(record.yearDate==null){
          this.yearDate='';
        } else {
          this.yearDate = record.yearDate+'年';
        }
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'category', 'serverValue','serverName','cpu','hardpan','disk','elements','expense','yearDate' ))
        });
      },
      handleCancel () {
        this.close()
      },
      handleSubmit () {
        debugger
        let serVal=this.$refs.serverName.value;
        let name="";
        if(serVal!="" || serVal !=undefined){
          name=this.$refs.serverName.dictOptions[serVal].text;
        }
        let cpuVa =this.$refs.cpu.value;
        let diskVa = this.$refs.disk.value;
        let hardpanVa = this.$refs.hardpan.value;
        let year=this.yearDate.toString();
        if(year.endsWith("年")){
          year= year.substring(0,4)
        }
        let val=this.$refs.serverName.value;
        let cpu="";
        let disk="";
        let hardpan="";
        let param="";
        if(null != cpuVa && cpuVa!="" && cpuVa !=undefined){
           cpu = cpuVa+"-";
          param=cpu;
        }
        if(diskVa!="" && diskVa!=null && diskVa !=undefined){
          disk =  diskVa +"-";
          param+=disk;
        }
        if(hardpanVa!="" &&  hardpanVa!=null &&  hardpanVa !=undefined){
          hardpan =  hardpanVa;
          param+=hardpan;
        }
        if(val=='5'){
          param='定制配置云CPU';
        }else if(val=='6'){
          param='定制配置云内存';
        }else if(val=='7'){
          param='定制配置云硬盘';
        }
        let  params={params:param};
        let yearDate={yearDate: year};
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            values.serverName=name;
            let formData = Object.assign(this.model, values,params,yearDate);

            let obj;

            if(!this.model.id){
              obj=saveServerPriceList(formData);
            }else{
              obj=editServerPriceList(formData);
            }
            obj.then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;

              that.close();
            })

          }
        })
      },
    }
  }
</script>

<style scoped>

</style>